#!/bin/bash
#author: <fanyuantao>
#date: 2024-08-08
#desc: This script is used to check and restart firewalld service if it is down.

function check_server_status() {
  while true; do

    if [ $(docker ps -a | grep Exited | wc -l) -eq 0 ]; then
      echo "no exited container"
      exit 1
    fi

    echo ""
    echo "There are exited containers:"
    echo ""
    docker ps -a | grep Exited | awk '{print $NF}'
    echo ""
    echo " 1.restart exited containers"
    echo " 2.rm exited containers"
    echo " 0.exit"
    echo ""
    read -p "Please choose an option:" option
    case $option in
      1) docker ps -a | grep Exited | awk '{print $NF}' | xargs -I {} docker start {};;
      2) docker ps -a | grep Exited | awk '{print $1}' | xargs -I {} docker rm {};;
      0) exit 1;;
      *) echo "Invalid option";;
    esac
  done
}

function main() {
  check_server_status
}

main